<?php
/**
 * 商品控制器
 * 
 * @version $Id: Item.php 488 2012-11-05 07:42:30Z linsir123 $
 */
class controllerItem extends AppController 
{
	/**
	 * @var 模型
	 */
	public $mItem;
	
	/**
	 * @var 模型
	 */
	public $mNav;
	
	/**
	 * @var 模型
	 */
	public $mShop;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		
		///
		$this->mItem = M::item();
		$this->mNav = M::nav();
		$this->mShop = M::shop();
	}
	
	/**
	 * 商品明细
	 */
	public function actionInfo()
	{
		$iId = $this->_getIid();
		
		///
		$item = $this->mItem->getInfo($iId);
		if (empty($item)) 
			$this->miss();
			
		///
		$shop = $this->mShop->getOne($item['nick']);
		$category = $this->mNav->getOne($item['cid']);
		$data = $this->mItem->getTaobaokes(array('show'=>5,'cid'=>$item['cid'],
			'sort'=>'commissionVolume_desc'));
			
		///
		View::display('item/index');
		View::assignSet(array(
			'item' => $item,
			'shop' => $shop, 
			'items' => $data[0], 
			'category' => $category, 
			'location' => $this->mNav->getLocation($item['cid']), 
		));
		
		///
		AppView::addJs('jquery.validate.js', '_e35go/plugin/validate.js', 'jquery.lazyload.js');
		AppView::addJs('_e35go/plugin/page_size.js', '_e35go/item.js');
		AppView::addCss('themes/01/item.css');
	}
	
	/**
	 * 页面跳转
	 */
	public function actionJump()
	{
		$iId = $this->_getIid();
		$uId = empty(M::user()->uId) ? '' : M::user()->uId;
		
		///
		View::display('item/jump');
		View::assignSet(array(
			'id' => $iId,
			'uId' => $uId
		));
	}
	
	/**
	 * 取得淘宝评论列表
	 */
	function actionComment()
	{
		$page = 1;
		$uId = trim($_GET['user_id']);
		$iId = trim($_GET['num_iid']);
		$domain = App::getRequest()->domain;
		
		///
		$url="http://rate.taobao.com/detail_rate.htm?userNumId=". $uId ."&auctionNumId=". $iId ."&showContent=2&currentPage=". $page ."&ismore=1&siteID=7";
		$content = file_get_contents($url);
		$content = str_replace('TB.detailRate = ', '', $content);
		$content = trim(mb_convert_encoding($content, "utf-8", "gb2312"));
		$list = json_decode($content, true);
		
		/// 页数  分数  打分次数  评价人数
		$totalPage = $list['rateListInfo']['paginator']['items'];
		$totalPage <= 100 or $totalPage = 100;
		echo $totalPage, "@";
		echo $list['scoreInfo']['merchandisScore'], "@";
		echo $list['scoreInfo']['merchandisTotal'], "@";
		echo $list['rateListInfo']['paginator']['items'], "@";
			
		/// 主体
		echo '<ul id="comment" class="clearfix">';
		if (is_array($list['rateListInfo']['rateList'])) {
			foreach ($list['rateListInfo']['rateList'] as $v) {
				echo '<li class="clearfix"><div class="left">',$v['rateContent'];
				echo '<br><font color="#999999">[',$v['rateDate'],']</font></div>';
				echo '<div class="right">买家：',$v['displayUserNick'];
				if($v['displayRatePic'] != '') 
					echo '<br><img src="', $domain, 'images/level/', $v['displayRatePic'], '">';
					
				echo '</div></li>';
			}
		}
		echo '</ul>';
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 取得商品ID
	 */
	private function _getIid()
	{
		$iId = trim($_GET['iid']);
		
		///
		if ( ! preg_match("/^[\d]+$/", $iId) || ! is_numeric($iId)) 
			$this->miss();
			
		return $iId;
	}
}
